﻿using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaserAutoTest.UI.Views.Mains
{
    /// <summary>
    /// BaiduView.xaml 的交互逻辑
    /// </summary>
    public partial class BaiduView : UserControl
    {
        public BaiduView()
        {
            InitializeComponent();

            var retuslt = CoreWebView2Environment.GetAvailableBrowserVersionString();//检测是否已安装WebView2环境

            //.ContinueWith( _ =>
            //{
            //    webBrowser.CoreWebView2.AddWebResourceRequestedFilter("*", Microsoft.Web.WebView2.Core.CoreWebView2WebResourceContext.All);
            //    
            //});
        }
        private async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            await webBrowser.EnsureCoreWebView2Async();

            webBrowser.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
            await webBrowser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage(window.document.URL);");
            //CoreWebView2.PostWebMessageAsString 或 CoreWebView2.PostWebMessageAsJson 方法发送消息给 WebView2 控件时，这个事件监听器会被触发
            await webBrowser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.addEventListener(\\'message\\', event => alert(event.data));\"");

            webBrowser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

            webBrowser.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
        }
        /// <summary>
        /// 在网页通过 window.chrome.webview.postMessage 方法发送消息给宿主应用程序时触发。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CoreWebView2_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
        {
            
        }

        /// <summary>
        /// 会在 WebView2 控件中的网页请求 Web 资源（例如，加载图片、脚本、样式表或其他类型的文件）时触发。这个事件允许宿主应用程序拦截并修改 Web 资源的请求和响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CoreWebView2_WebResourceRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs e)
        {
            webBrowser.CoreWebView2.Navigate("http://192.168.29.20:5238/Account/Login");
            webBrowser.CoreWebView2.ExecuteScriptAsync("javascriptFunction()");
        }

        
        /// <summary>
        /// 在网页中的脚本尝试打开一个新窗口或标签页时触发的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
        {
            e.NewWindow = (CoreWebView2)sender;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            if(webBrowser.CoreWebView2!=null)
            {
                JObject json = new JObject();
                json["Name"] = "小明";

                webBrowser.CoreWebView2.PostWebMessageAsJson(JsonConvert.SerializeObject(json));
            }
        }
    }
}
